<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../assets/js/vue.js"></script>
    <title>v-for</title>
</head>
<body>
    <div id="app">
        <ul>
            <li v-for="item in sortItems">
                {{item}}
            </li>
        </ul>
        <br>
        <hr>
        sort students by age:
        <ol>
            <li v-for="(student,index) in sortStudents">
                {{student.name}}---{{student.age}}, the index is{{index}}
            </li>
        </ol>
    </div>
        <!--computed里新声明了一个对象sortItems，
            如果不重新声明会污染原来的数据源，这是Vue不允许的，
            所以你要重新声明一个对象-->
    <script>
        var app = new Vue({
            el:'#app',
            data:{
                items : [62,35,6,57,76,4,43],

                students:[
                    {name:'zhangSan',age:89},
                    {name:'liSi',age:45},
                    {name:'wangWu',age:67},
                    {name:'luBenWei',age:2},
                    {name:'PDD',age:889}
                ]
            },

            
            computed:{
                sortItems:function(){
                    return this.items.sort(sortNumber);
                },

                sortStudents:function(){
                    return sortByKey(this.students,'age');
                }
            }
        });

        
        function sortNumber(a,b){
            return a-b;
        }

        function sortByKey(array,key){
            return array.sort(function(a,b){
            var x=a[key];
            var y=b[key];
            return ((x<y)?-1:((x>y)?1:0));
        });
        }
    </script>
    
</body>
</html>